/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package asa;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Mickael
 */
public class ASAExprAffect extends ASAExpr implements NeedBlocInterface {

    private ASAExprIdent id;
    private ASAExpr e;
    private ASABloc bl = null;
    private boolean isArray = false;
    private boolean isGlobal = false;
    private int index = 0;

    public ASAExprAffect(ASAExpr id, ASAExpr e) {
        this(id,e,false);

    }

    public ASAExprAffect(ASAExpr id, ASAExpr e, boolean isGlobal) {
        try {
            this.id = (ASAExprIdent)id;
        } catch(ClassCastException ex) {
            System.err.println("Parse error: syntax is ID = expression");
            System.exit(1);
        }
        this.e = e;
        this.isGlobal = isGlobal;
        isArray = this.id.getIndex()!=null;
    }

    public void setBl(ASABloc bl) {
        if (!isGlobal) {
            this.bl = bl;
        }
        if(isArray){
           for(ASAExpr e : id.getIndex()){
                e.setBl(bl);
           }
        }
        super.setBl(bl);
    }


    @Override
    public ASAExpr getExpr1() {
        return id;
    }

    @Override
    public ASAExpr getExpr2() {
        return e;
    }

    @Override
    public int evalue() {
        int val = e.evalue();
        String ident = id.getNomVar();
        Variables v = Variables.getInstance();
        if (isArray) {
           List<Integer> indices = new ArrayList<Integer>();
           for(ASAExpr e : id.getIndex()){
               indices.add(e.evalue());
           }
           v.setValue(bl, ident, indices, val);
        } else {
            v.setValue(bl, ident, val);
        }
        return val;
    }

    @Override
    public String getLabelExplorateur() {
        return "AFFECT";
    }
}
